
#include "TextureManager.h"
#include "Texture.h"

TextureManager::~TextureManager()
{
	TexturesByName::iterator i = textures.begin();
	while(i != textures.end())
	{
		Texture* t = i->second;
		if(t != NULL)
			delete t;
		i++;
	}
	textures.clear();
}

const Texture* TextureManager::getTextureByName( const std::string &fname ) 
{
	// Ver si ya la tengo
	TexturesByName::iterator i = textures.find( fname );

	// Devolverla si existe
	if( i != textures.end( ) ) 
		return i->second;
	
	Texture* text = new Texture(fname.c_str());
	
	// registrarla
	textures[ fname ] = text;

	// devolverla
	return text;
}

void TextureManager::unloadTextures()
{
   	TexturesByName::iterator i = textures.begin();
	while(i != textures.end())
	{
		Texture* t = i->second;
		if(t != NULL)
			delete t;
		i++;
	}
	textures.clear(); 
}
